import { isNumber, toString } from "lodash";
import numeral from "numeral";

function numberFormat(value, decimalPoints, decimalDelimiter, thousandsDelimiter) {
    // Temporarily update locale data (restore defaults after formatting)
  const locale = numeral.localeData();
  const savedDelimiters = locale.delimiters;

  // Mimic old behavior - AngularJS `number` filter defaults:
  // - `,` as thousands delimiter
  // - `.` as decimal delimiter
  // - three decimal points
  locale.delimiters = {
    thousands: ",",
    decimal: ".",
  };
  let formatString = "0,0.000";

  if (!decimalPoints) decimalPoints = 0;
  if (!decimalDelimiter) decimalDelimiter = ".";
  if (!thousandsDelimiter) thousandsDelimiter = '';
  
  if ((Number.isFinite(decimalPoints) && decimalPoints >= 0) || decimalDelimiter || thousandsDelimiter) {
    locale.delimiters = {
      thousands: thousandsDelimiter,
      decimal: decimalDelimiter || ".",
    };

    formatString = "0,0";
    if (decimalPoints > 0) {
      formatString += ".";
      while (decimalPoints > 0) {
        formatString += "0";
        decimalPoints -= 1;
      }
    }
  }
  const result = numeral(value).format(formatString);

  locale.delimiters = savedDelimiters;
  return result;
}

// 0 - special case, use first record
// 1..N - 1-based record number from beginning (wraps if greater than dataset size)
// -1..-N - 1-based record number from end (wraps if greater than dataset size)
function getRowNumber(index, rowsCount) {
  var row = parseInt(index, 10) || 0;
  if (row < 0) row = 0;
  if (row >= rowsCount) row = rowsCount-1;

  return row;
}

function formatValue(value, { stringPrefix, stringSuffix, stringDecimal, stringDecChar, stringThouSep }) {
  if (!!stringDecimal && parseInt(stringDecimal)>0) {
    if (!isNumber(value)) value = parseFloat(value);
    value = numberFormat(value, stringDecimal, stringDecChar, stringThouSep);
    return toString(stringPrefix) + value + toString(stringSuffix);
  } else {
    return toString(value);
  }
}

//返回一行
function getRowValue(row, colNameArr, options) {
  let arr = [];

  if (!!colNameArr) {
    const indexKeys = Object.keys(row);
    for (var i = 0; i < colNameArr.length; i++) {
      if (colNameArr[i] === '0' || !isNaN(Number(colNameArr[i]))) {
        let indexCol = parseInt(colNameArr[i]);
        if (indexCol >= indexKeys.length) indexCol = indexKeys.length-1;
        if (indexCol < 0) indexCol = 0;

        const indexKey = indexKeys[indexCol];
        arr.push(formatValue(row[indexKey], options)); 
      } else {
        arr.push(formatValue(row[colNameArr[i]], options)); 
      }
    }
  } else {
    //所有列
    for (var key in row) {
      arr.push(formatValue(row[key], options)); 
    }
  }

  if (arr.length === 1) return arr[0];
  if (!!options.tableData) return "<td>" + arr.join("</td><td>") + "</td>";
  if (!!options.numberArray) return "[" + arr.join(",") + "]";
  return "['" + arr.join("','") + "']";
}

export function getCounterData(rows, options) {
  const rowsCount = rows.length;

  //统计行数
  if (options.countRow) {
    return formatValue(rowsCount, options);
  }

  //无记录
  if (rowsCount === 0) {
    if (!!options.tableData) return '';
    return '空';
  }  
    
  const colNameArr = !!options.colName && options.colName.toString().split(',');
  
  //单行
  if (options.hasOwnProperty("rowNumber") && options.rowNumber.toString().indexOf("-")<=0 && options.rowNumber.toString().indexOf(",")<=0) {
    let rowNumber = getRowNumber(options.rowNumber, rowsCount);
    return getRowValue(rows[rowNumber], colNameArr, options);
  } 

  let result;
  var arr = [];
  if (options.hasOwnProperty("rowNumber")) { 
    var arrIndex = options.rowNumber.toString().split(',');
    for (var i = 0; i < arrIndex.length; i++) {
      var strIndex = arrIndex[i];
      if (strIndex.indexOf("-") > 0) {
        let startIndex = getRowNumber(strIndex.split("-")[0], rowsCount);
        let closeIndex = getRowNumber(strIndex.split("-")[1], rowsCount);
        for (var j = startIndex; j <= closeIndex; j++) {
          arr.push(getRowValue(rows[j], colNameArr, options));
        }
      } else {
        let intIndex = getRowNumber(strIndex, rowsCount);  
        arr.push(getRowValue(rows[intIndex], colNameArr, options));
      }            
    }
  } else {
    //所有行
    for (var i = 0; i < rowsCount; i++) {
      arr.push(getRowValue(rows[i], colNameArr, options));
    }
  }

  if (!!options.tableData) 
    result = "<tr>" + arr.join('</tr><tr>') + "</tr>";
  else 
    result = "[" + arr.join(',') + "]";

  return result;  
}


